﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using D2DEngine.Scene;
using Microsoft.Xna.Framework.Graphics;
using D2DEngine.Physics;
using D2DEngine.Components;

namespace D2DEngine.Core
{
    public abstract class D2DGame : Microsoft.Xna.Framework.Game
    {
        protected GraphicsDeviceManager graphics;
        protected SpriteBatch spriteBatch;

        protected D2DSceneManager sceneManager;

        public D2DGame()
        {
            this.graphics = new GraphicsDeviceManager(this);           

            Content.RootDirectory = "Content";
            IsFixedTimeStep = false;

            // Initialize the game settings
            InitGameSettings();

            // Window dimensions
            this.graphics.PreferredBackBufferWidth = D2DSettings.ScreenWidth;
            this.graphics.PreferredBackBufferHeight = D2DSettings.ScreenHeight;
        }

        protected override void LoadContent()
        {
            this.spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);

            LoadRessources();
            LoadAssets();
        }


        protected override void Initialize()
        {
            // Load content first
            base.Initialize();

            this.sceneManager = D2DSceneManager.Instance;

            LoadGame();           
        }
        
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; 

            // Update the input manager
            D2DInputManager.Instance.Update();

            // Update the scene manager
            this.sceneManager.Update(gameTime); 

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the scenes
            this.sceneManager.Draw(this.spriteBatch);

            base.Draw(gameTime);
        }


        protected abstract void LoadRessources();
        protected abstract void LoadAssets();
        protected abstract void LoadGame();

        protected abstract void InitGameSettings();
    }
}
